<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"zhengmingpei.gitee.io","root":"/","images":"/images","scheme":"Pisces","darkmode":false,"version":"8.8.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="安装MySQL检查本机是否安装了MySQL： mysql --version  如果已经安装了，输出应该大致如下： mysql  Ver 14.14 Distrib 5.5.37, for debian-linux-gnu (x86_64) using readline 6.2">
<meta property="og:type" content="article">
<meta property="og:title" content="MySQL学习笔记1">
<meta property="og:url" content="https://zhengmingpei.gitee.io/2016/09/20/MySQL-Note1/index.html">
<meta property="og:site_name" content="郑明培的个人博客">
<meta property="og:description" content="安装MySQL检查本机是否安装了MySQL： mysql --version  如果已经安装了，输出应该大致如下： mysql  Ver 14.14 Distrib 5.5.37, for debian-linux-gnu (x86_64) using readline 6.2">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2016-09-20T07:00:00.000Z">
<meta property="article:modified_time" content="2021-11-28T03:23:59.368Z">
<meta property="article:author" content="zhengmingpei">
<meta property="article:tag" content="Mysql">
<meta property="article:tag" content="数据库">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://zhengmingpei.gitee.io/2016/09/20/MySQL-Note1/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://zhengmingpei.gitee.io/2016/09/20/MySQL-Note1/","path":"2016/09/20/MySQL-Note1/","title":"MySQL学习笔记1"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>MySQL学习笔记1 | 郑明培的个人博客</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">郑明培的个人博客</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="home fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives" rel="section"><i class="archive fa-fw"></i>归档</a></li>
        <li class="menu-item menu-item-about"><a href="/about" rel="section"><i class="user fa-fw"></i>关于</a></li>
        <li class="menu-item menu-item-categories"><a href="/categories" rel="section"><i class="th fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags" rel="section"><i class="tags fa-fw"></i>标签</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%89%E8%A3%85MySQL"><span class="nav-number">1.</span> <span class="nav-text">安装MySQL</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#MySQL%E8%B4%A6%E6%88%B7%E7%AE%A1%E7%90%86"><span class="nav-number">2.</span> <span class="nav-text">MySQL账户管理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#ROOT%E8%B4%A6%E6%88%B7%E5%AF%86%E7%A0%81%E8%AE%BE%E7%BD%AE"><span class="nav-number">2.1.</span> <span class="nav-text">ROOT账户密码设置</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%A4%9A%E6%9D%A1%E8%AF%AD%E5%8F%A5%E8%AE%BE%E7%BD%AE"><span class="nav-number">2.1.1.</span> <span class="nav-text">多条语句设置</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%8D%95%E6%9D%A1%E8%AF%AD%E5%8F%A5%E8%AE%BE%E7%BD%AE"><span class="nav-number">2.1.2.</span> <span class="nav-text">单条语句设置</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%A3%80%E6%9F%A5%E4%BF%AE%E6%94%B9%E7%BB%93%E6%9E%9C"><span class="nav-number">2.1.3.</span> <span class="nav-text">检查修改结果</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%99%AE%E9%80%9A%E7%94%A8%E6%88%B7%E5%AF%86%E7%A0%81%E8%AE%BE%E7%BD%AE"><span class="nav-number">2.2.</span> <span class="nav-text">普通用户密码设置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%A0%E9%99%A4%E8%B4%A6%E6%88%B7"><span class="nav-number">2.3.</span> <span class="nav-text">删除账户</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">zhengmingpei</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives">
          <span class="site-state-item-count">36</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories">
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags">
        <span class="site-state-item-count">41</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/zhengmingpei" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;zhengmingpei" rel="noopener" target="_blank"><i class="github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://gitee.com/zhengmingpei" title="gitee → https:&#x2F;&#x2F;gitee.com&#x2F;zhengmingpei" rel="noopener" target="_blank">gitee</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://space.bilibili.com/32918983" title="bilibili → https:&#x2F;&#x2F;space.bilibili.com&#x2F;32918983" rel="noopener" target="_blank">bilibili</a>
      </span>
  </div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://zhengmingpei.gitee.io/2016/09/20/MySQL-Note1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="zhengmingpei">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="郑明培的个人博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          MySQL学习笔记1
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2016-09-20 15:00:00" itemprop="dateCreated datePublished" datetime="2016-09-20T15:00:00+08:00">2016-09-20</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-11-28 11:23:59" itemprop="dateModified" datetime="2021-11-28T11:23:59+08:00">2021-11-28</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%9C%A8%E5%8C%97%E4%BA%AC/" itemprop="url" rel="index"><span itemprop="name">在北京</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="安装MySQL"><a href="#安装MySQL" class="headerlink" title="安装MySQL"></a>安装MySQL</h2><p>检查本机是否安装了MySQL：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">mysql --version</span><br></pre></td></tr></table></figure>

<p>如果已经安装了，输出应该大致如下：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">mysql  Ver 14.14 Distrib 5.5.37, <span class="keyword">for</span> debian-linux-gnu (x86_64) using readline 6.2</span><br></pre></td></tr></table></figure>
<span id="more"></span>

<p>如果没安装，就需要到官网下载对应版本的安装包了，安装是需要权限的。</p>
<p>在ubuntu下的安装方法是：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">sudo apt-get update <span class="comment">#更新包仓库</span></span><br><span class="line">sudo apt-get install mysql-server <span class="comment">#安装mysql</span></span><br></pre></td></tr></table></figure>

<h2 id="MySQL账户管理"><a href="#MySQL账户管理" class="headerlink" title="MySQL账户管理"></a>MySQL账户管理</h2><h3 id="ROOT账户密码设置"><a href="#ROOT账户密码设置" class="headerlink" title="ROOT账户密码设置"></a>ROOT账户密码设置</h3><p>默认账户root是没有密码的，所以在终端直接执行：<code>mysql</code> 即可进入MySQL的交互界面，就像输入<code>python</code>可以进入python的解释器。</p>
<p>有必要给root账户设置密码，官网文档给出了几种设置方法：</p>
<h4 id="多条语句设置"><a href="#多条语句设置" class="headerlink" title="多条语句设置"></a>多条语句设置</h4><p>shell下执行：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">mysql -u root</span><br></pre></td></tr></table></figure>

<p>然后在MySQL的交互界面输入以下四条语句，带分号，密码填入你想要使用的密码。</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">SET</span> PASSWORD <span class="keyword">FOR</span> <span class="string">&#x27;root&#x27;</span>@<span class="string">&#x27;localhost&#x27;</span> <span class="operator">=</span> PASSWORD(<span class="string">&#x27;new_password&#x27;</span>);</span><br><span class="line"><span class="keyword">SET</span> PASSWORD <span class="keyword">FOR</span> <span class="string">&#x27;root&#x27;</span>@<span class="string">&#x27;127.0.0.1&#x27;</span> <span class="operator">=</span> PASSWORD(<span class="string">&#x27;new_password&#x27;</span>);</span><br><span class="line"><span class="keyword">SET</span> PASSWORD <span class="keyword">FOR</span> <span class="string">&#x27;root&#x27;</span>@<span class="string">&#x27;::1&#x27;</span> <span class="operator">=</span> PASSWORD(<span class="string">&#x27;new_password&#x27;</span>);</span><br><span class="line"><span class="keyword">SET</span> PASSWORD <span class="keyword">FOR</span> <span class="string">&#x27;root&#x27;</span>@<span class="string">&#x27;host_name&#x27;</span> <span class="operator">=</span> PASSWORD(<span class="string">&#x27;new_password&#x27;</span>);</span><br></pre></td></tr></table></figure>

<h4 id="单条语句设置"><a href="#单条语句设置" class="headerlink" title="单条语句设置"></a>单条语句设置</h4><p>同样要先进MySQL的交互界面，执行：</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line">UPDATE mysql.user <span class="keyword">SET</span> Password <span class="operator">=</span> PASSWORD(<span class="string">&#x27;new_password&#x27;</span>)</span><br><span class="line">    <span class="operator">-</span><span class="operator">&gt;</span>     <span class="keyword">WHERE</span> <span class="keyword">User</span> <span class="operator">=</span> <span class="string">&#x27;root&#x27;</span>;</span><br></pre></td></tr></table></figure>

<p>还没完，接下来执行：</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line">FLUSH PRIVILEGES;</span><br></pre></td></tr></table></figure>

<h4 id="检查修改结果"><a href="#检查修改结果" class="headerlink" title="检查修改结果"></a>检查修改结果</h4><p>同样要先进MySQL的交互界面，执行：</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="keyword">User</span>, Host, Password <span class="keyword">FROM</span> mysql.user;</span><br></pre></td></tr></table></figure>

<p>未修改的话，其输出将类似于：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">+------+--------------------+----------+</span><br><span class="line">| User | Host               | Password |</span><br><span class="line">+------+--------------------+----------+</span><br><span class="line">| root | localhost          |          |</span><br><span class="line">| root | myhost.example.com |          |</span><br><span class="line">| root | 127.0.0.1          |          |</span><br><span class="line">| root | ::1                |          |</span><br><span class="line">|      | localhost          |          |</span><br><span class="line">|      | myhost.example.com |          |</span><br><span class="line">+------+--------------------+----------+</span><br></pre></td></tr></table></figure>

<p>修改之后，最后一栏的输出将会出现密码，当然，是对原密码做过处理的无规律的“密码”。</p>
<h3 id="普通用户密码设置"><a href="#普通用户密码设置" class="headerlink" title="普通用户密码设置"></a>普通用户密码设置</h3><p>跟给ROOT账户设置的方法是一样的，不过要先用ROOT账户登录，输入上面改过的密码。</p>
<p>另外，要注意，之前要添加相应的账户才能进行设置。至于如何添加账户，以后会讲，这里假设已经新建了一个账户，名称为username。</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">mysql -u root -p</span><br></pre></td></tr></table></figure>

<p>之后的设置:</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">SET</span> PASSWORD <span class="keyword">FOR</span> <span class="string">&#x27;username&#x27;</span>@<span class="string">&#x27;localhost&#x27;</span> <span class="operator">=</span> PASSWORD(<span class="string">&#x27;new_password&#x27;</span>);</span><br><span class="line"><span class="keyword">SET</span> PASSWORD <span class="keyword">FOR</span> <span class="string">&#x27;username&#x27;</span>@<span class="string">&#x27;host_name&#x27;</span> <span class="operator">=</span> PASSWORD(<span class="string">&#x27;new_password&#x27;</span>);</span><br></pre></td></tr></table></figure>

<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line">UPDATE mysql.user <span class="keyword">SET</span> Password <span class="operator">=</span> PASSWORD(<span class="string">&#x27;new_password&#x27;</span>)</span><br><span class="line">    <span class="operator">-</span><span class="operator">&gt;</span>     <span class="keyword">WHERE</span> <span class="keyword">User</span> <span class="operator">=</span> <span class="string">&#x27;username&#x27;</span>;</span><br><span class="line">FLUSH PRIVILEGES;</span><br></pre></td></tr></table></figure>

<h3 id="删除账户"><a href="#删除账户" class="headerlink" title="删除账户"></a>删除账户</h3><p>同样要先进MySQL的交互界面，执行：</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">DROP</span> <span class="keyword">USER</span> <span class="string">&#x27;&#x27;</span>@<span class="string">&#x27;localhost&#x27;</span>;</span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">USER</span> <span class="string">&#x27;&#x27;</span>@<span class="string">&#x27;host_name&#x27;</span>;</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Mysql/" rel="tag"># Mysql</a>
              <a href="/tags/%E6%95%B0%E6%8D%AE%E5%BA%93/" rel="tag"># 数据库</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2016/09/19/Markdown%E8%AF%AD%E6%B3%95%E8%AF%B4%E6%98%8E/" rel="prev" title="Markdown语法说明">
                  <i class="fa fa-chevron-left"></i> Markdown语法说明
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2016/09/21/MySQL-Note2/" rel="next" title="MySQL学习笔记2">
                  MySQL学习笔记2 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">zhengmingpei</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/pisces/" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
